# Maintainer: Hoang Nguyen <folliekazetani@protonmail.com>
pkgname=tetragon-client
pkgver=1.1.2
pkgrel=0
pkgdesc="CLI client for eBPF-based Security Observability and Runtime Enforcement"
url="https://github.com/cilium/tetragon"
arch="x86_64 aarch64" # fails to build on other platforms
license="Apache-2.0"
makedepends="go"
checkdepends="linux-headers"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/cilium/tetragon/archive/refs/tags/v$pkgver.tar.gz"
builddir="$srcdir/tetragon-$pkgver"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	go build -v \
		-ldflags "-X github.com/cilium/tetragon/pkg/version.Version=v$pkgver" \
		./cmd/tetra

	for shell in bash fish zsh; do
		./tetra completion $shell > tetra.$shell
	done
}

check() {
	# Some tests access example files inside the repo, so no -trimpath.
	# /tests/e2e: only run unittest
	# /bpf/tests: requires root privilege to run
	# /pkg/reader/proc: read process name at PID 1
	# /pkg/selectors: read values in kernel Pid namespace
	# /pkg/alignchecker: fails to build bpf/objs/bpf_alignchecker.o
	# /pkg/sensors/{tracing,exec}, /pkg/{tracepoint,btf,bugtool}: try to access /sys/kernel
	# /pkg/sensors/exec/procevents, /pkg/cgroups: fail on CI (lacking cgroup controller "pids")
	# /pkg/pidfile: try to create /var/run/tetragon directory
	# shellcheck disable=2046
	GOFLAGS="${GOFLAGS/-trimpath/}" go test $(go list ./... | grep -v \
		-e '/bpf/tests$' \
		-e '/pkg/alignchecker$' \
		-e '/pkg/btf$' \
		-e '/pkg/bugtool$' \
		-e '/pkg/cgroups$' \
		-e '/pkg/pidfile$' \
		-e '/pkg/reader/proc$' \
		-e '/pkg/selectors$' \
		-e '/pkg/sensors/exec$' \
		-e '/pkg/sensors/exec/procevents$' \
		-e '/pkg/sensors/tracing$' \
		-e '/pkg/tracepoint$' \
		-e '/tests/e2e' \
		)
}

package() {
	install -Dm755 tetra -t "$pkgdir"/usr/bin/

	install -Dm644 tetra.bash \
		"$pkgdir"/usr/share/bash-completion/completions/tetra
	install -Dm644 tetra.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/tetra.fish
	install -Dm644 tetra.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_tetra
}

sha512sums="
129dce49845936a5be5f7f610aaa3acec5e8329bf8cc26e7f6f4157a7e1047a047b231e799f9e8f695a45030258ef418828c54064e4f26fd1fd79c36bf6afc62  tetragon-client-1.1.2.tar.gz
"
